<?php

declare(strict_types=1);
/**
 * This file is part of micro-share.
 *
 * @link     https://www.cppo.cn/
 * @contact  1300188034@qq.com
 * @license  https://gitee.com/cppo/micro_share/blob/master/README.md
 */

namespace App\Rpc\Trait;

use App\Model\CateModel;

trait CateTraitRpc
{

    /**
     * 获取Cate列表.
     */
    public function getCateList(array $param = []): array
    {
        $map = [];
        if (! empty($param['where'])) {
            $filter = $param['where'];
            if (! isset($filter['status'])) {
                $map[] = ['status', '=', intval($filter['status'])];
            }
            $map[] = ['app_id', '=', intval($filter['app_id'] ?? 0)];
        }
        $builder = CateModel::query()->where($map)->orderBy($param['order'] ?? 'id', $param['sort'] ?? 'DESC');
        $items = $builder->get();

        if (empty($items)) {
            return $this->output->success();
        }
        $list = [];
        foreach ($items as $model) {
            if (! $model instanceof CateModel || ! $model->id) {
                continue;
            }
            $list[] = [
                'id' => $model->id,
                'app_id' => $model->app_id,
                'name' => $model->name,
                'icon' => $model->icon,
                'status' => $model->status,
                'sort' => $model->sort,
                'create_time' => $model->create_time,
                'update_time' => $model->update_time,
            ];
        }
        return $this->output->success([
            'list' => $list,
            'total' => count($list),
        ]);
    }
}
